<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>选择文件</title>
	<link rel = "icon" href = "Developer.ico">
	<style>
		* {
			cursor: default;
		}
		body {
			background-color: rgb(58, 58, 58);
			color: #fff;
			user-select: none;
		}
		input {
			background-color: rgb(58, 58, 58);
			color: #fff;
			border: none;
			border-bottom: 1px solid rgb(143, 143, 143);
			outline: none;
			height: 30px;
			width: calc(100% - 30px);
		}
		.pathNode {
			position: relative;
			padding-left: 10px;
		}
		.folderOpen {
			display: inline-block;
			width: 15px;
			text-align: center;
		}
		.pathNameDiv {
			cursor: pointer;
		}
		.pathNameDiv:hover {
			background-color: rgb(119, 167, 255);
		}
		input[type='date'] {
			width: 120px;
		}
		button {
			width: 50px;
			height: 30px;
			background-color: rgb(78, 78, 78);
			color: #fff;
			border: none;
			cursor: pointer;
		}
		button:focus {outline:none;}
	</style>
  </head>
<body oncontextmenu="return false;">
	
	<div id="fileTree">
	</div>
	<div>
		<input id="path" readonly/>
		<button onclick="setPath();">确定</button>
	</div>
</body>
<script>

	var fs = require( 'fs' );
	var process = require('child_process');

	// 取系统磁盘
	var diskInfo = process.execSync('wmic logicaldisk get caption', {encoding: 'utf8'});
	var diskArray = getDiskList(diskInfo);
	//var diskArray = ['c:', 'd:', 'f:'];
	for (var i = 0; i < diskArray.length; i++) {
		var fileTree = document.getElementById("fileTree");
		var diskDiv = document.createElement("div");
		diskDiv.classList.add("rootPath");
		var pathNameDiv = document.createElement("div");
		var directorySpan = document.createElement("span");
		var pathNameSpan = document.createElement("span");
		directorySpan.innerHTML = "+";
		directorySpan.classList.add("folderOpen");
		pathNameSpan.innerHTML = diskArray[i];
		pathNameDiv.appendChild(directorySpan);
		pathNameDiv.appendChild(pathNameSpan);
		pathNameDiv.onclick = loadFolder;
		pathNameDiv.classList.add("pathNameDiv");
		diskDiv.path = diskArray[i];
		diskDiv.appendChild(pathNameDiv);
		diskDiv.isDirectory = true;
		fileTree.appendChild(diskDiv);
	}
	
	
	function getDiskList(diskInfo) {
		var diskArray = [];
		var rows = diskInfo.split("\n");
		for (var i = 0; i < rows.length; i++) {
			if (rows[i].length > 1 && rows[i].substr(1, 1) == ":") {
				diskArray.push(rows[i].substr(0, 2));
			}
		}
		return diskArray;
	}
	
	function loadFolder() {
		if (this.parentNode.loaded) {
			if (this.parentNode.open) {
				this.parentNode.lastElementChild.style.display = 'none';
				this.parentNode.open = false;
				this.firstChild.innerHTML = "+";
			} else {
				this.parentNode.lastElementChild.style.display = 'block';
				this.parentNode.open = true;
				this.firstChild.innerHTML = "-";
			}
		} else {
			if (this.parentNode.isDirectory) {
				var subNodeDiv = document.createElement("div");
				subNodeDiv.classList.add("subNode");
				//var folderArray = ['aaa', 'bbb', 'ccc'];
				var folderArray = [];
				if (fs.existsSync(this.parentNode.path)) {
					folderArray = fs.readdirSync(this.parentNode.path + "/");
				}
				for (var i = 0; i < folderArray.length; i++) {
					var pathDiv = document.createElement("div");
					pathDiv.classList.add("pathNode");
					pathDiv.path = this.parentNode.path + "/" + folderArray[i];
					try {
						if (fs.lstatSync(pathDiv.path).isDirectory()) {
							pathDiv.isDirectory = true;
						} else {
							pathDiv.isDirectory = false;
						}
					} catch (e) {
						pathDiv.isDirectory = false;
					}
					var selectType = getQueryVariable("selectType");
					if (selectType == "folder" && !pathDiv.isDirectory) {
						pathDiv.style.display = "none";
					}
					var pathNameDiv = document.createElement("div");
					var directorySpan = document.createElement("span");
					var pathNameSpan = document.createElement("span");
					if (pathDiv.isDirectory) {
						directorySpan.innerHTML = "+";
					} else {
						directorySpan.innerHTML = " ";
					}
					directorySpan.classList.add("folderOpen");
					pathNameSpan.innerHTML = folderArray[i];
					pathNameDiv.appendChild(directorySpan);
					pathNameDiv.appendChild(pathNameSpan);
					pathNameDiv.onclick = loadFolder;
					pathNameDiv.classList.add("pathNameDiv");
					pathDiv.appendChild(pathNameDiv);
					subNodeDiv.appendChild(pathDiv);
				}
				this.firstChild.innerHTML = "-";
				this.parentNode.appendChild(subNodeDiv);
				this.parentNode.loaded = true;
				this.parentNode.open = true;
			}
		}
		document.getElementById("path").value = this.parentNode.path;
	}
	var win = nw.Window.get();
	var parentWindow;
	function setParentWindow(parentWin) {
		parentWindow = parentWin;
	}
	function setPath() {
		// parentWindow.window.setPath(document.getElementById("path").value, getQueryVariable("inputId"));
		parentWindow.window.document.getElementById(getQueryVariable("inputId")).value = document.getElementById("path").value;
		win.close();
	}
	
	function getQueryVariable(variable) {
		var query = window.location.search.substring(1);
		var vars = query.split("&");
		for (var i=0;i<vars.length;i++) {
			var pair = vars[i].split("=");
			if(pair[0] == variable){return pair[1];}
		}
		return(false);
	}
</script>
</html>